package com.example.mytestdemo.customviewbook.two

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import com.example.mytestdemo.R

/**
 * @author tgw
 * @date 2023/2/8
 * @describe 折叠的view
 */
class PolytoPolyViewView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : View(context, attrs, defStyleAttr) {

    var bitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.cat)
    val mMatrix: Matrix = Matrix()
    var sTransHeight = 100f
    var mFoldWidth = 0
    var spiltNum = 8

//    init {
//        mFoldWidth = width/spiltNum
//        var src = floatArrayOf(
//            0f,
//            0f,
//            bitmap.width.toFloat(),
//            0f,
//            bitmap.width.toFloat(),
//            bitmap.height.toFloat(),
//            0f,
//            bitmap.height.toFloat()
//        )
//        var dst = floatArrayOf(
//            0f,
//            0f,
//            bitmap.width.toFloat(),
//            sTransHeight,
//            bitmap.width.toFloat(),
//            bitmap.height.toFloat() + sTransHeight,
//            0f,
//            bitmap.height.toFloat()
//        )
//        mMatrix.setPolyToPoly(src, 0, dst, 0, src.size shr 1)
//
//    }

        override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mFoldWidth = width/spiltNum
        var src = floatArrayOf(0f,0f,bitmap.width.toFloat(),0f,bitmap.width.toFloat(),bitmap.height.toFloat(),0f,bitmap.height.toFloat())
        var dst = floatArrayOf(0f,0f,bitmap.width.toFloat(),sTransHeight,bitmap.width.toFloat(),bitmap.height.toFloat()+sTransHeight,0f,bitmap.height.toFloat())
        mMatrix.setPolyToPoly(src,0,dst,0,src.size shr 1)
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.save()
        val rect = Rect(0, 0, mFoldWidth, getHeight());
        canvas.setMatrix(mMatrix)
        canvas.clipRect(rect)
        canvas.drawBitmap(bitmap, 0f, 0f, null)
        canvas.restore()
    }


}